package model

import (
	"fmt"
	"gorm.io/gorm"
	"time"
)

// User 用户表
type User struct {
	gorm.Model
	Username       string    `json:"username" gorm:"unique;not null;comment:'账号'"`
	Password       string    `json:"password" gorm:"not null;comment:'密码'"`
	Priority       uint8     `json:"priority" gorm:"comment:'权限等级'"`
	Customer       string    `json:"customer" gorm:"comment:'客户名字'"`
	UserId         int       `json:"userId" gorm:"comment:'用户id'"`
	Email          string    `json:"email" gorm:"comment:'邮箱'"`
	Phone          string    `json:"phone" gorm:"comment:'手机号'"` // 确保长度不超过数据库定义的长度
	EmailValidated bool      `json:"email_validated" gorm:"default:0;comment:'邮箱是否已验证'"`
	PhoneValidated bool      `json:"phone_validated" gorm:"default:0;comment:'手机号是否已验证'"`
	SignupAt       time.Time `json:"signup_at" gorm:"comment:'注册日期'"`
	LastActive     time.Time `json:"last_active" gorm:"comment:'最后活跃时间戳'"`
	ProfileText    string    `json:"profile" gorm:"comment:'用户属性'"`
	Status         int       `json:"status" gorm:"not null;default:0;comment:'账户状态(启用/禁用/锁定/标记删除等)'"`
}

func InitializeUserData(db *gorm.DB) error {
	var count int64

	// 检查表中是否已经有数据
	db.Model(&User{}).Count(&count)
	if count > 0 {
		fmt.Println("CategoryData Table already has data, skipping initialization.")
		return nil
	}
	//没有则创建管理员用户，用户名admin，密码admin，权限等级0，客户名为大合，用户id为1001
	var userData = User{
		Username:   "admin",
		Password:   "daheadmin",
		Priority:   0,
		Customer:   "Charles",
		UserId:     1001,
		SignupAt:   time.Now(),
		LastActive: time.Now(),
	}
	result := db.Create(&userData)
	if result.Error != nil {
		return result.Error
	}

	fmt.Println("Admin user created successfully.")
	return nil
}
